﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Windows.Forms;

public partial class UC_MessageCenterPopup : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ListView1.DataSource = loadSenders("jens");
        ListView1.DataBind();
        
        if (Page.IsPostBack)
        {
            
            //ModalPopupExtender1.Show();
            
        }
        
    }

    private void createList(string Username)
    {
        
    }

    protected IQueryable loadSenders(string Username)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        var listOfConvesations = from c in db.Conversations
                                 where c.Datenight.aspnet_User.UserName == Username || c.aspnet_User.UserName == Username
                                 select new { username = whichPersonToSelect_Username(c, Username), Profile = whichPersonToSelect_profilPic( c, Username), date = calcDate(c.lastTimeMessage) };

        return listOfConvesations;
    }

    private String whichPersonToSelect_Username(Conversation c, string userName)
    {
        if (c.aspnet_User.UserName == userName)
        {
            return c.Datenight.aspnet_User.UserName;
        }
        else
        {
            return c.aspnet_User.UserName;
        }
    }

    private String whichPersonToSelect_profilPic(Conversation c, string userName)
    {
        if (c.aspnet_User.UserName == userName)
        {
            return setProfilePicture(c.Datenight.aspnet_User.UserName);
        }
        else
        {
            return setProfilePicture(c.aspnet_User.UserName);
        }
    }

    private String calcDate(DateTime message)
    {
        if (message.Day == DateTime.Today.Day)
        {
            return message.ToString("HH:mm");
        }
        else
        {
            string s = "";
            s += message.Date.ToString("dd");
            s += ". ";
            s += whatMonth(message.Month);
            return s;
        }
    }

    private string whatMonth(int monthNumber)
    {
        if (monthNumber == 1)
        {
            return "Januar";
        }
        else if (monthNumber == 2)
        {
            return "febuar";
        }
        else if (monthNumber == 3)
        {
            return "Marts";
        }
        else if (monthNumber == 4)
        {
            return "April";
        }
        else if (monthNumber == 5)
        {
            return "Maj";
        }
        else if (monthNumber == 6)
        {
            return "Juni";
        }
        else if (monthNumber == 7)
        {
            return "Juli";
        }
        else if (monthNumber == 8)
        {
            return "August";
        }
        else if (monthNumber == 9)
        {
            return "September";
        }
        else if (monthNumber == 10)
        {
            return "Oktober";
        }
        else if (monthNumber == 11)
        {
            return "November";
        }
        else if (monthNumber == 12)
        {
            return "December";
        }
        else
        {
            return "Fejl";
        }
    }

    public void test(object sender, EventArgs e)
    {
        MessageBox.Show("SOMETHING");
    }

    public void linkbutton_clicked(object sender, EventArgs e)
    {
        LinkButton lBtn = (LinkButton)sender;
        string username = lBtn.CommandArgument;
        Send.CommandArgument = username;
        loadMessages(username);
    }

    public void loadMessages(string username)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        var listOfMessages = from m in db.Messages
                             from c in db.Conversations
                             where c.Datenight.aspnet_User.UserName == username || c.aspnet_User.UserName == username
                             where c.Datenight.aspnet_User.UserName == Membership.GetUser().UserName || c.aspnet_User.UserName == Membership.GetUser().UserName
                             where m.conversationId == c.conversationId
                             select new { username = m.aspnet_User.UserName, timeOfMessage = calcDate(Convert.ToDateTime(m.timeOfMessage)), textMessage = m.Text, profile = setProfilePicture(m.aspnet_User.UserName) };

        ListView2.DataSource = listOfMessages;
        ListView2.DataBind();
        //Timer1.Enabled = true;
        
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            ModalPopupExtender1.Show();
            loadMessages(Send.CommandArgument);
        }        
    }

    public void insertConIdAsCmdArgumentOnButton(int conId)
    {
        Send.CommandArgument = conId.ToString();
    }

    public void newMessage(object sender, EventArgs e)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        Conversation con = (from c in db.Conversations
                            where c.Datenight.aspnet_User.UserName == Send.CommandArgument || c.aspnet_User.UserName == Send.CommandArgument
                            where c.Datenight.aspnet_User.UserName == Membership.GetUser().UserName || c.aspnet_User.UserName == Membership.GetUser().UserName
                            select c).Single();
        aspnet_User user = db.aspnet_Users.SingleOrDefault(u => u.UserName == Membership.GetUser().UserName);

        Message mess = new Message();
        mess.timeOfMessage = DateTime.Now;
        con.lastTimeMessage = (DateTime)mess.timeOfMessage;
        mess.Text = TextBox1.Text;
        mess.isRead = false;
        mess.conversationId = con.conversationId;
        mess.Conversation = con;
        mess.aspnet_User = user;
        mess.userName = user.UserName;
        db.Messages.InsertOnSubmit(mess);
        db.SubmitChanges();
        TextBox1.Text = "skriv en besked";
        
    }

    private string setProfilePicture(string username)
    {
        string path = Server.MapPath("../Dateiaften1/images/ProfilePictures/");
        if (File.Exists(path + username + ".png"))
        {
            return "../Dateiaften1/images/ProfilePictures/" + username + ".png";
        }
        else
        {
            return "../Dateiaften1/images/ProfilePictures/" + "default.png";
        }
    }
}